<?php

namespace Admin;

use Application\ModelAbstract;

class Model extends ModelAbstract
{
    const ADMIN = 'administrator';
    const USER  = 'user';

    protected $table;

    public function __construct()
    {
        $this->table = 'user';
        parent::__construct();
    }

    public function insert(array $user)
    {
        $data = $this->prepare($user);
        return $this->getDb()->insert('user', $data);
    }

    public function update($id, $user)
    {
        if ($update = $this->prepare($user)) {
            return $this->getDb()->update('user', $update, ['id = ?' => $id]);
        }
        return false;
    }

    protected function prepare(array $user)
    {
        if ($user['password']) {
            $user['salt'] = uniqid(mt_rand(), true);
            $user['password'] = sha1($user['password'] . $user['salt']);
        }
        foreach ($user as $key => $value) {
            if (!$value) {
                unset($user[$key]);
            }
        }
        // $user = array_filter($user);
        return $user;
    }
}
